package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportNewsRpcService;
import com.hunttown.mes.rpc.domain.TransportNewsDTO;
import com.hunttown.mes.rpc.domain.query.TransportNewsDTOQuery;
import com.hunttown.mes.service.TransportNewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.TransportNewsConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:13
 */
@Service
public class TransportNewsRpcServiceImpl implements TransportNewsRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportNewsRpcServiceImpl.class);

    private final TransportNewsService transportNewsService;

    @Autowired
    public TransportNewsRpcServiceImpl(TransportNewsService transportNewsService) {
        this.transportNewsService = transportNewsService;
    }

    @Override
    public TransportNewsDTO insert(TransportNewsDTO transportNewsDTO) {
        try {
            return convert(transportNewsService.insert(convert(transportNewsDTO)));
        } catch (Exception e) {
            logger.error("TransportNews create failed.", e);
            throw new RuntimeException("TransportNews create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportNewsDTO transportNewsDTO) {
        try {
            return transportNewsService.updateInfoById(convert(transportNewsDTO));
        } catch (Exception e) {
            logger.error("TransportNews update by id failed.", e);
            throw new RuntimeException("TransportNews update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportNewsDTOQuery transportNewsDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportNewsDTOQuery.getW_id() != null && transportNewsDTOQuery.getW_id() > 0) {
                query.put("w_id", transportNewsDTOQuery.getW_id());
            }

            if (transportNewsDTOQuery.getIds() != null && !transportNewsDTOQuery.getIds().equals("")) {
                query.put("ids", transportNewsDTOQuery.getIds());
            }

            if (transportNewsDTOQuery.getHits() != null) {
                query.put("hits", transportNewsDTOQuery.getHits());
            }

            if (transportNewsDTOQuery.getUpdateHits() != null) {
                query.put("updateHits", transportNewsDTOQuery.getUpdateHits());
            }

            if (transportNewsDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportNewsDTOQuery.getStopFlag());
            }

            if (transportNewsDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportNewsDTOQuery.getDeleteFlag());
            }

            return transportNewsService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportNews update by query failed.", e);
            throw new RuntimeException("TransportNews update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportNewsDTO selectById(Integer id) {
        try {
            return convert(transportNewsService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportNews select by id failed.", e);
            throw new RuntimeException("TransportNews select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportNewsDTO selectObjByQuery(TransportNewsDTOQuery transportNewsDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportNewsDTOQuery.getEntId() != null) {
                query.put("entId", transportNewsDTOQuery.getEntId());
            }

            if (transportNewsDTOQuery.getShortTitle() != null) {
                query.put("shortTitle", transportNewsDTOQuery.getShortTitle());
            }

            if (transportNewsDTOQuery.getCatalogId() != null) {
                query.put("catalogId", transportNewsDTOQuery.getCatalogId());
            }

            if (transportNewsDTOQuery.getStatus() != null) {
                query.put("status", transportNewsDTOQuery.getStatus());
            }

            if (transportNewsDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportNewsDTOQuery.getStopFlag());
            }

            if (transportNewsDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportNewsDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportNewsDTOQuery.getOrderBy())) {
                query.put("orderBy", transportNewsDTOQuery.getOrderBy());
            }

            return convert(transportNewsService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportNews select obj by query failed.", e);
            throw new RuntimeException("TransportNews select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportNewsDTO> getListForPage(TransportNewsDTOQuery transportNewsDTOQuery) {
        Query query = Query.page(transportNewsDTOQuery.getPn(), transportNewsDTOQuery.getPageSize());

        if (transportNewsDTOQuery.getId() != null && transportNewsDTOQuery.getId() > 0) {
            query.put("id", transportNewsDTOQuery.getId());
        }

        if (transportNewsDTOQuery.getIds() != null && !transportNewsDTOQuery.getIds().equals("")) {
            query.put("ids", transportNewsDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportNewsDTOQuery.getLikeName())) {
            query.put("likeName", transportNewsDTOQuery.getLikeName());
        }

        if (transportNewsDTOQuery.getBeforeId() != null) {
            query.put("beforeId", transportNewsDTOQuery.getBeforeId());
        }

        if (transportNewsDTOQuery.getAfterId() != null) {
            query.put("afterId", transportNewsDTOQuery.getAfterId());
        }

        if (transportNewsDTOQuery.getCatalogId() != null && transportNewsDTOQuery.getCatalogId() != -9999) {
            query.put("catalogId", transportNewsDTOQuery.getCatalogId());
        }

        if (transportNewsDTOQuery.getCateIds() != null && !transportNewsDTOQuery.getCateIds().equals("")) {
            query.put("cateIds", transportNewsDTOQuery.getCateIds());
        }

        if (transportNewsDTOQuery.getStatus() != null) {
            query.put("status", transportNewsDTOQuery.getStatus());
        }

        if (transportNewsDTOQuery.getAdminId() != null) {
            query.put("adminId", transportNewsDTOQuery.getAdminId());
        }

        if (StringUtils.isNotBlank(transportNewsDTOQuery.getShowDate())) {
            query.put("showDate", transportNewsDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportNewsDTOQuery.getStartDate())) {
            query.put("startDate", transportNewsDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportNewsDTOQuery.getEndDate())) {
            query.put("endDate", transportNewsDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportNewsDTOQuery.getOrderBy())) {
            query.put("orderBy", transportNewsDTOQuery.getOrderBy());
        }

        if (transportNewsDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportNewsDTOQuery.getStopFlag());
        }

        if (transportNewsDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportNewsDTOQuery.getDeleteFlag());
        }

        List<TransportNewsDTO> list = convert(transportNewsService.selectByQuery(query));
        Page<TransportNewsDTO> page = Page.from(transportNewsDTOQuery.getPn(), transportNewsDTOQuery.getPageSize(), list);
        page.setTotalItems(transportNewsService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportNewsService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportNews delete failed.", e);
            throw new RuntimeException("TransportNews delete failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> getMapDataByQuery(TransportNewsDTOQuery transportNewsDTOQuery, String element) {
        try {
            Query query = Query.page(1, 5);

            if (transportNewsDTOQuery.getCateIds() != null && !transportNewsDTOQuery.getCateIds().equals("")) {
                query.put("cateIds", transportNewsDTOQuery.getCateIds());
            }

            query.put("stopFlag", 0);
            query.put("deleteFlag", 0);

            return transportNewsService.getMapDataByQuery(query, element);
        } catch (Exception e) {
            logger.error("TransportNews select map data failed.", e);
            throw new RuntimeException("TransportNews select map data failed, Please order your code.", e);
        }
    }

    @Override
    public Long getTotalCount(TransportNewsDTOQuery transportNewsDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportNewsDTOQuery.getStartDate())) {
                query.put("startDate", transportNewsDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(transportNewsDTOQuery.getEndDate())) {
                query.put("endDate", transportNewsDTOQuery.getEndDate());
            }

            query.put("stopFlag", 0);
            query.put("deleteFlag", 0);
            return transportNewsService.countByQuery(query);
        } catch (Exception e) {
            logger.error("TransportNews get total count failed.", e);
            throw new RuntimeException("TransportNews select get total count failed, Please order your code.", e);
        }
    }
}